package com.alibaba.nacos.example.spring.cloud.util;

import org.springframework.stereotype.Component;

import com.alibaba.nacos.example.spring.cloud.config.SmsConfig;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

@Component
public class SmsUtil {
	
	/**
	 * @param 验证码为：${code}，您正在注册成为平台会员，感谢您的支持！
	 * @param PhoneNumbers
	 * @return
	 * @throws Exception
	 */
	public static boolean registerSmsTool(String PhoneNumbers) throws Exception {
		DefaultProfile profile = DefaultProfile.getProfile(SmsConfig.ALIYUN_REGIONID, SmsConfig.ALIYUN_ACCESS_KEY_ID,SmsConfig.ALIYUN_ACCESS_KEY_SECRET);
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", SmsConfig.ALIYUN_REGIONID);
		request.putQueryParameter("PhoneNumbers", PhoneNumbers);
		request.putQueryParameter("SignName", "蚂蚁咖啡");
		request.putQueryParameter("TemplateCode", "SMS_166372450");
		String code = AESUtil.generateCode(6);
		request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * @param 您的动态码为：${code}，您正在进行密码重置操作，如非本人操作，请忽略本短信！
	 * @param PhoneNumbers
	 * @return
	 * @throws Exception
	 */
	public static boolean ResetSmsTool(String PhoneNumbers) throws Exception {
		DefaultProfile profile = DefaultProfile.getProfile(SmsConfig.ALIYUN_REGIONID, SmsConfig.ALIYUN_ACCESS_KEY_ID,SmsConfig.ALIYUN_ACCESS_KEY_SECRET);
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", SmsConfig.ALIYUN_REGIONID);
		request.putQueryParameter("PhoneNumbers", PhoneNumbers);
		request.putQueryParameter("SignName", "蚂蚁咖啡");
		request.putQueryParameter("TemplateCode", "SMS_166372448");
		String code = AESUtil.generateCode(6);
		request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return true;
	}
}
